package com.cool.collect.springcode.a01;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;

/**
 * @author xiemiao
 * @description
 * @date 2023/08/25
 */
@SpringBootApplication
public class A01Application {
    public static void main(String[] args) throws Exception {
        //  applicationContext组合并扩展beanFactory的功能
        ConfigurableApplicationContext context = SpringApplication.run(A01Application.class, args);

        Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        singletonObjects.setAccessible(true);
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
        for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
            System.out.println(stringObjectEntry.getKey() + "=" + stringObjectEntry.getValue());
        }

        // 国际化功能
        String message = context.getMessage("hi", null, Locale.CHINA);
        System.out.println(message);
        // 获取资源功能
        Resource[] resources = context.getResources("classpath*:META-INF/spring.factories");
        for (Resource resource : resources) {
            System.out.println(resource);
        }
        // 获取配置信息功能
        System.out.println(context.getEnvironment().getProperty("JAVA17_HOME"));
        System.out.println(context.getEnvironment().getProperty("server.port"));

        // 发送事件功能
        context.publishEvent("");

    }
}
